/*
 *
 * ImageScroller - a Image Horizental Scroll Viewer
 * Version 0.1
 * @requires jQuery v1.2.1
 *
 * Copyright (c) 2007 Luan
 * Email verycss-ok@yahoo.com.cn
 *
 * Dual licensed under the MIT and GPL licenses:
 * http://www.opensource.org/licenses/mit-license.php
 * http://www.gnu.org/licenses/gpl.html
 *
 */

jQuery.fn.imageScroller = function(params) {
    var p = params || {
            next:".prev",
            prev:".next",
            frame:".home-x-roll-content",
            wrap : ".home-x-roll-wrap",
            child:".thumbnail"
        };
    var _btnNext = $(p.next);
    var _btnPrev = $(p.prev);
    var _imgFrame = $(p.frame);
    var _frameWrap = $(p.wrap);
    var _child = p.child;

    var _imgFrameWidth = _imgFrame.width();
    var _frameWrapWidth = _frameWrap.width();
    var _curPos = parseInt($(_imgFrame).css('left'));
    var _itemWidth = $(_child).outerWidth();
    var _itv;

    var turnLeft = function() {
        if(_imgFrameWidth + _curPos > _frameWrapWidth) {
            _imgFrame.animate({"left": _curPos - _itemWidth}, 'fast', '', function () {
                _curPos = parseInt($(_imgFrame).css('left'));
            });
        } else {

        }
    };

    var turnRight = function() {
        if(_curPos<0) {
            _imgFrame.animate({"left": _curPos + _itemWidth}, 'fast', '', function () {
                _curPos = parseInt($(_imgFrame).css('left'));
            });
        }
    };

    _btnNext.click(turnRight);
    _btnPrev.click(turnLeft);

    $(window).on('resize', function () {
        _frameWrap = $(p.wrap);
    })
};